package com.koko.controller;

import com.koko.game.entity.GameDetail;
import com.koko.result.Result;
import com.koko.service.GameDetailService;
import com.koko.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/game")
public class GameDetailController {
    @Autowired
    private GameDetailService gameDetailService;

    @PutMapping("/admin/detail")
    @CacheEvict(value = "gameCache", allEntries = true)
    public Result<String> updateGameDetail(
            @RequestParam("gameDetailInfo") String gameDetailInfo,
            @RequestParam(value="widerPicture", required = false) MultipartFile widerPicture,
            @RequestParam(value="higherPicture", required = false) MultipartFile higherPicture,
            @RequestParam(value="simplePicture", required = false) MultipartFile simplePicture,
            @RequestParam(value="post", required = false) MultipartFile post) {
        GameDetail gameDetail = JSONUtil.parseObject(gameDetailInfo, GameDetail.class);
        gameDetailService.update(gameDetail, widerPicture, higherPicture, simplePicture, post);
        return Result.success();
    }

    @PostMapping("admin/detail")
    @CacheEvict(value = "gameCache", allEntries = true)
    public Result<String> addGameDetail(
            @RequestParam("gameDetailInfo") String gameDetailInfo,
            @RequestParam(value="widerPicture", required = false) MultipartFile widerPicture,
            @RequestParam(value="higherPicture", required = false) MultipartFile higherPicture,
            @RequestParam(value="simplePicture", required = false) MultipartFile simplePicture,
            @RequestParam(value="post", required = false) MultipartFile post) {
        GameDetail gameDetail = JSONUtil.parseObject(gameDetailInfo, GameDetail.class);
        gameDetailService.add(gameDetail, widerPicture, higherPicture, simplePicture, post);
        return Result.success();
    }

    /**
     * feign接口，通过gameId查询gameDetail信息
     * @param gameId
     * @return
     */
    @GetMapping("/detail/{gameId}")
    public Result<GameDetail> getGameDetailByGameId(@PathVariable Long gameId) {
        GameDetail gameDetail = gameDetailService.getByGameId(gameId);
        return Result.success(gameDetail);
    }
}
